//
//  TSRegisterViewController.m
//  chart
//
//  Created by xu on 15/7/26.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSRegisterViewController.h"
#import "TSRegisterModel.h"

#define MARGIN_TOP_REGISTER      24
#define MARGIN_LEFT_REGISTER     24
#define FLD_HEIGHT      41
#define BTN_HEIGHT      56

@implementation TSRegisterViewController{
    UITextField             *userNameField;
    UITextField             *passwordField;
    UITextField             *phoneNumField;
    UIButton                *registerBtn;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"注册";
    self.view.backgroundColor = RGB(239, 240, 244);
    
    [self addPhoneAndPwdView];
    [self addRegisterBtn];
    
    /* 加入收键盘手势 */
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleField:)];
    [self.view addGestureRecognizer:tap];
}

- (void)handleField:(UIGestureRecognizer*)gesture{
    for (UIView * view in self.view.subviews) {
        if ([view isFirstResponder]) {
            [view resignFirstResponder];
        }
    }
}

/* 加入手机号和密码输入框 */
- (void)addPhoneAndPwdView
{
    userNameField = [[UITextField alloc] init];
    userNameField.placeholder = @"请输入用户名";
    userNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self setUpFieldProperties:userNameField];
    userNameField.keyboardType = UIKeyboardTypeASCIICapable;
    [self.view addSubview:userNameField];
    
    passwordField = [[UITextField alloc] init];
    passwordField.placeholder = @"请输入密码";
    passwordField.secureTextEntry  = YES;
    passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self setUpFieldProperties:passwordField];
    passwordField.keyboardType = UIKeyboardTypeASCIICapable;
    [self.view addSubview:passwordField];
    
    phoneNumField = [[UITextField alloc] init];
    phoneNumField.placeholder = @"请输入手机号";
    phoneNumField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self setUpFieldProperties:phoneNumField];
    phoneNumField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneNumField];
}

/* 加入注册按钮 */
- (void)addRegisterBtn
{
    registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.titleLabel.font = FONT(12);
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:[UIColor grayColor]];
    [registerBtn.layer setCornerRadius:5.f];
    [registerBtn addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    userNameField.frame = CGRectMake(MARGIN_LEFT_REGISTER,
                                     MARGIN_TOP_REGISTER,
                                     currentDeviceWidth-2*MARGIN_LEFT_REGISTER,
                                     FLD_HEIGHT);
    passwordField.frame = CGRectMake(MARGIN_LEFT_REGISTER,
                                     CGRectGetMaxY(userNameField.frame)+MARGIN_TOP_REGISTER,
                                     CGRectGetWidth(userNameField.frame),
                                     FLD_HEIGHT);
    phoneNumField.frame = CGRectMake(MARGIN_LEFT_REGISTER,
                                     CGRectGetMaxY(passwordField.frame)+MARGIN_TOP_REGISTER,
                                     CGRectGetWidth(userNameField.frame),
                                     FLD_HEIGHT);
    registerBtn.frame = CGRectMake(MARGIN_LEFT_REGISTER, CGRectGetMaxY(phoneNumField.frame)+MARGIN_TOP_REGISTER, CGRectGetWidth(passwordField.frame), FLD_HEIGHT);
}

/* 注册用户 */
- (void)registerUser:(UIButton *)btn
{
    WS(weakSelf);
    [[TSUserModel sharedInstance] registerWithUser:userNameField.text andPassword:passwordField.text andPhone:phoneNumField.text completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
        
        if (isSucceeded) {
            [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

/* 设置textField属性 */
- (void)setUpFieldProperties:(UITextField *)field{
    field.backgroundColor =[UIColor whiteColor];
    field.tintColor = [UIColor grayColor];
    field.font = FONT(12);
    field.layer.cornerRadius = 5.f;
    field.layer.borderColor = [UIColor grayColor].CGColor;
    field.layer.borderWidth = .5f;
//    field.delegate = self;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 5)];
    field.leftView = view;
    field.leftViewMode = UITextFieldViewModeAlways;
}

@end
